<?php
namespace app\backstage\controller;
use think\Controller;
use think\Db;
use think\facade\Request;

class Api extends Controller
{
    protected $ip = ['47.98.126.1','106.6.127.210'];

    /*
     * 书籍列表
     * */
    public function books()
    {
        if (!in_array(request()->ip(), $this->ip)) {
            return json(['code' => 101, 'message' => 'IP受限'.request()->ip(), 'data' => []]);
        }

        $domain = config('img_domain');

        /*$pnids = Db::table('novel_tmp')->column('party_novel_id');
        $novel_ids = Db::table('novel_mp_party')->where('party_code','siwei')
            ->where('party_novel_id','in',$pnids)->column('novel_id');*/
        $list = Db::table('novel')
            ->where('in_shelf',1)
            ->field('id,title,origin_title,cover,author,freq_id,class_id,son_class_id,update_status,brief,number_words')
            ->select();

        $son_class_id = array_unique(array_column($list,'son_class_id'));
        $sons = Db::table('novel_class_son')->where('id','in',$son_class_id)->column('id,name');

        foreach ($list as $k=>$v)
        {
            $list[$k]['son_name'] = isset($sons[$v['son_class_id']]) ? $sons[$v['son_class_id']] : '';
            $list[$k]['cover'] = $domain.$v['cover'];
        }

        return json(['code'=>0,'msg'=>'success','data'=>$list]);
    }

    /*
     * 章节列表获取
     * */
    public function chapters()
    {
        if (!in_array(request()->ip(), $this->ip)) {
            return json(['code' => 101, 'message' => 'IP受限'.request()->ip(), 'data' => []]);
        }

        $params = Request::param('', '', 'strip_tags');
        $verify_result = $this->validate($params, 'Api.chapters');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $page   = isset($params['page']) ? $params['page'] : 1;
        $limit  = isset($params['limit']) ? $params['limit'] : 300;

        $list = Db::table('novel_chapter')->where('novel_id',$params['nid'])->page($page,$limit)
            ->field('novel_id,chapter_no,is_pay,words,name')->select();
        foreach ($list as $k=>$v)
        {
            $content = Db::table('novel_chapter_content_'. intval(substr($v['novel_id'], -2)))
                ->where('novel_id',$v['novel_id'])->where('chapter_no',$v['chapter_no'])->field('content')->findOrEmpty();
            $list[$k]['content'] = $content ? $content['content'] : '';
        }

        return json(['code'=>0,'msg'=>'success','data'=>$list]);
    }

    /*书籍订单数据汇总*/
    public function book_amount()
    {
        if (!in_array(request()->ip(), $this->ip)) {
            return json(['code' => 101, 'message' => 'IP受限'.request()->ip(), 'data' => []]);
        }
        
        $params = Request::param('', '', 'strip_tags');
        $verify_result = $this->validate($params, 'Api.book_amount');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $where[] = ['read_date','=',$params['date']];
        $list = Db::table('rp_novel_date_daily_discount')->where($where)
            ->field('sum(money) as money,nid')->group('nid')->select();

        return json(['code'=>0,'msg'=>'success','data'=>$list]);
    }

}